package org.spark.java.transformation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaPairRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.VoidFunction;
import scala.Tuple2;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * cogroup:对两个RDD中的KV元素，每个RDD中相同key中的元素分别聚合成一个集合。
 * 与reduceByKey不同的是针对两个RDD中相同的key的元素进行合并。
 *
 * @param:
 * @return:
 * @auther: 某人的目光
 * @date: 2020年1月16日19:59:33
 */
public class Cogroup {

    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf();
        //设置运行环境
        sparkConf.setMaster("local");
        sparkConf.setAppName("cogroup");
        JavaSparkContext context = new JavaSparkContext(sparkConf);

        List<Tuple2<Integer, String>> list1 = Arrays.asList(
                new Tuple2<Integer, String>(1, "某人的目光"),
                new Tuple2<Integer, String>(2, "桜花抄"),
                new Tuple2<Integer, String>(3, "幻听")
        );
        List<Tuple2<Integer, Integer>> list2 = Arrays.asList(
                new Tuple2<Integer, Integer>(1, 99),
                new Tuple2<Integer, Integer>(2, 80),
                new Tuple2<Integer, Integer>(3, 50),
                new Tuple2<Integer, Integer>(1, 50),
                new Tuple2<Integer, Integer>(2, 70),
                new Tuple2<Integer, Integer>(3, 65)
        );
        JavaPairRDD<Integer, String> rdd1 = context.parallelizePairs(list1);
        JavaPairRDD<Integer, Integer> rdd2 = context.parallelizePairs(list2);
        JavaPairRDD<Integer, Tuple2<Iterable<String>, Iterable<Integer>>> cogroup = rdd1.cogroup(rdd2);

        cogroup.foreach(new VoidFunction<Tuple2<Integer, Tuple2<Iterable<String>, Iterable<Integer>>>>() {
            public void call(Tuple2<Integer, Tuple2<Iterable<String>, Iterable<Integer>>> t) throws Exception {
                System.out.println("编号:" + t._1);
                System.out.println("名称集合:" + t._2._1);
                System.out.println("分数集合:" + t._2._2);
            }
        });
    }
}


